﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContentManagementAdministrator.Dialogs
{
    public partial class FormLoadImage : Form
    {
        private Size crop;
        private Point start = new Point();
        private Size selection = new Size();
        private Image image;

        public FormLoadImage()
        {
            InitializeComponent();
        }

        public Image Image
        {
            get { return image; }
        }

        public bool Execute(Size crop)
        {
            this.crop = crop;
            this.selection = crop;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
                return ShowDialog() == DialogResult.OK;
            }
            return false;
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (!selection.IsEmpty)
            {
                e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), start.X, start.Y, selection.Width, selection.Height);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            start = e.Location;
            selection = new Size();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int width = e.X - start.X;
                int height = width * crop.Height / crop.Height;
                selection = new Size(width, height);
                pictureBox1.Refresh();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float ratio = (float)pictureBox1.Width / (float)pictureBox1.Image.Width;
            ratio = Math.Min(ratio, (float)pictureBox1.Height / (float)pictureBox1.Image.Height);

            SizeF imgSize = new SizeF((float)pictureBox1.Image.Width * ratio, (float)pictureBox1.Image.Height * ratio);

            PointF cropStart = new PointF((-((float)pictureBox1.Width - imgSize.Width) / 2 + (float)start.X) / ratio,
                                    (-((float)pictureBox1.Height - imgSize.Height) / 2 + (float)start.Y ) / ratio);

            SizeF cropSelection = new SizeF((float)selection.Width / ratio, (float)selection.Height / ratio);

            Bitmap bmp = new Bitmap(crop.Width, crop.Height);
            Graphics g = Graphics.FromImage(bmp);

            Rectangle cropSrc = new Rectangle((int)cropStart.X, (int)cropStart.Y, (int)cropSelection.Width, (int)cropSelection.Height);

            g.DrawImage(pictureBox1.Image, new Rectangle(new Point(0,0), crop), cropSrc , GraphicsUnit.Pixel);

            this.image = bmp;

            this.DialogResult = DialogResult.OK;
        }
    }
}
